package com.pesking.core.sqlparser.processor;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.DateUtils;
import com.pesking.core.util.LogUtils;

public class CurrentTimestampSqlParameterHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "当前日期时间参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle() + "赋值当前日期时间";
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"CURRENT_TIMESTAMP"});
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		sbSQL.append("?");
		processors.add(sqlParameter);
	}

	@Override
	public void setParameter(PreparedStatement ps, Parameter parameter, int index,SqlParameter sqlParameter) throws SQLException {
		Timestamp ts = new Timestamp(new Date().getTime());
		LogUtils.info("当前时间->" + DateUtils.timestamp2String(ts));
		ps.setTimestamp(index, ts);
	}

	@Override
	public String getUsage() {
		return "[CURRENT_TIMESTAMP]";
	}
}
